/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.mapper;

import org.retabel.persistence.dao.document.DocumentConfigurationDao;
import org.retabel.persistence.dao.document.SectionConfigurationDao;
import org.retabel.persistence.entity.document.DocumentConfiguration;
import org.retabel.persistence.entity.document.DocumentConfigurationTypeEnum;
import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.document.SectionConfigurationTypeEnum;
import xsd.renderdocumentconfiguration.StaticDocument;
import xsd.renderdocumentconfiguration.StaticSection;

import javax.ejb.EJB;

/**
 * Abstract base class for implementing jaxb based document configuration marshaling
 *
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 15:44 12.08.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
public abstract class XmlDocumentConfigurationMapperBaseBean {

    /** Dao to access the top level configuration. */
    @EJB
    protected DocumentConfigurationDao documentConfigurationDao;

    /** Dao to access the section configurations. */
    @EJB
    protected SectionConfigurationDao sectionConfigurationDao;

    /**
     * Unmarshal the given document configuration xml
     *
     * @param documentConfigurationData the xml data as byte array
     * @return the document configuration instance
     * @throws XmlConfigurationMappingException if an error occurs
     */
    public abstract DocumentConfiguration unmarshal(byte[] documentConfigurationData) throws XmlConfigurationMappingException;

    /**
     * Maps a static document configuration part, defined in the xml configuration.
     *
     * @param staticDocument the static document configuration.
     * @return the persisted data set.
     * @throws XmlConfigurationMappingException
     *          if the static document doesn't exist.
     */
    protected DocumentConfiguration mapStaticDocumentConfiguration(StaticDocument staticDocument)
        throws XmlConfigurationMappingException {
        String ref = staticDocument.getRef();
        String version = staticDocument.getVersion();
        DocumentConfiguration documentConfiguration = documentConfigurationDao.findByNameVersionAndType(ref, version,
                                                                                                        DocumentConfigurationTypeEnum.PERMANENT);
        if (documentConfiguration == null) {
            throw new XmlConfigurationMappingException(
                "The document configuration " + ref + " version " + version + " does not exist!");
        }
        return documentConfiguration;
    }

    protected SectionConfiguration mapStaticSection(StaticSection staticSection)
        throws XmlConfigurationMappingException {
        String ref = staticSection.getRef();
        String version = staticSection.getVersion();
        SectionConfiguration sectionConfiguration = sectionConfigurationDao.findByNameVersionAndType(ref, version,
                                                                                                     SectionConfigurationTypeEnum.PERMANENT);
        if (sectionConfiguration == null) {
            throw new XmlConfigurationMappingException(
                "The section configuration " + ref + " version " + version + " does not exist!");
        }
        return sectionConfiguration;
    }

}
